package cn.howxu.yichen_ovo.registry;

import cn.howxu.yichen_ovo.Res;
import cn.howxu.yichen_ovo.YiChenOVO;
import cn.howxu.yichen_ovo.common.block.Doll;
import cn.howxu.yichen_ovo.common.block.HowXuDoll;
import cn.howxu.yichen_ovo.common.block.SpecialDoll;
import cn.howxu.yichen_ovo.common.block.YiChenDoll;
import cn.howxu.yichen_ovo.common.item.DollItem;
import cn.howxu.yichen_ovo.init.ModSounds;
import com.google.common.collect.Maps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegisterEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @description: TODO
 * @author: HowXu
 * @date: 2025/9/12 18:56
 */
@Mod.EventBusSubscriber(modid = YiChenOVO.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class DollRegistryEvent {

    public static final Map<String, List<String>> Jade_Plugin_tips = Maps.newLinkedHashMap();
    public static final Map<String, Integer> Jade_Plugin_levels = Maps.newLinkedHashMap();

    public static final Map<ResourceLocation, Doll> BLOCKS = Maps.newLinkedHashMap();
    public static final List<Item> ITEMS = new ArrayList<>();
    private static final List<DollRegisterHelper> dans = new LinkedList<>(List.of(
            // DollRegisterHelper.getDanFan("gotopark", 2, 2),
            DollRegisterHelper.getDanFan("gotopark_pt", 2, 2),
            DollRegisterHelper.getDanFan("xiao_y3", 2, 2),
            DollRegisterHelper.getDanFan("bury_love", 2, 2),
            DollRegisterHelper.getDanFan("bury_love_pt", 2, 2),
            DollRegisterHelper.getDanFan("big_yusen", 2, 3),
            DollRegisterHelper.getDanFan("big_yusen2", 2, 2),
            DollRegisterHelper.getDanFan("big_yusen_pt", 2, 2),
            DollRegisterHelper.getDanFan("riji_", 2, 2),
            DollRegisterHelper.getDanFan("yuanxi_19", 2, 2),
            DollRegisterHelper.getDanFan("suifengyao", 2, 2),
            // DollRegisterHelper.getDanFan("dchen233", 2, 2),
            DollRegisterHelper.getDanFan("yujiage", 2, 2),
            DollRegisterHelper.getDanFan("xingyu233", 2, 2),
            DollRegisterHelper.getDanFan("ohana_1234", 2, 2),
            DollRegisterHelper.getDanFan("kejilao", 0, 1),
            DollRegisterHelper.getDanFan("cold_kejilao", 0, 1)
    ));

    private static final List<DollRegisterHelper> YiChens = new LinkedList<>(List.of(
            DollRegisterHelper.getDanFan("yichen_mm", 1, 3),
            DollRegisterHelper.getDanFan("yichen_dan", 1, 2),
            DollRegisterHelper.getDanFan("yichen_nvwu", 1, 2),
            DollRegisterHelper.getDanFan("yichen_nvpu", 1, 2),
            DollRegisterHelper.getDanFan("yichen_xixi", 1, 2),
            DollRegisterHelper.getDanFan("yichen_puxin", 1, 2),
            DollRegisterHelper.getDanFan("yichen_angel", 1, 2),
            DollRegisterHelper.getDanFan("yichen_chachuqu", 1, 2),
            DollRegisterHelper.getDanFan("yichen_chachuqu2", 1, 2),
            DollRegisterHelper.getDanFan("yichen_zhale", 1, 2)
    ));
    private static final Logger log = LoggerFactory.getLogger(DollRegistryEvent.class);

    @SubscribeEvent
    public static void registerBlocks(RegisterEvent event) {
        // 批量注册玩偶
        if (event.getRegistryKey().equals(ForgeRegistries.BLOCKS.getRegistryKey())) {
            // 悲伤旋律忧郁枪
            ResourceLocation id =  Res.rl( "howxu");
            HowXuDoll block = new HowXuDoll();
            BLOCKS.put(id, block);
            event.register(ForgeRegistries.BLOCKS.getRegistryKey(), id, () -> block);
            
            // 空巷
            registerSpecialDoll(event,"gotopark",ModSounds.GOTOPARK.get());
            
            // 谶
            registerSpecialDoll(event,"dchen233",ModSounds.DCHEN.get());
            
            // 不呆
            
            registeYichens(YiChens,event);
            registeDans(dans,event); // 注册没有特殊音效的玩偶
        }
        // 为玩偶分别注册物品的特殊名称
        if (event.getRegistryKey().equals(ForgeRegistries.ITEMS.getRegistryKey())) {
            registerWithTips(event,"howxu",3,3);
            registerWithTips(event,"dchen233",3,2);
            registerWithTips(event,"gotopark",2,2);
            
            dans.forEach(helper -> {
                registerWithTips(event, helper.getName(),helper.getNeed_tips_size(), helper.getLevel());
            });
            YiChens.forEach(helper -> {
                registerWithTips(event, helper.getName(),helper.getNeed_tips_size(), helper.getLevel());
            });
            
        }
    }

    private static void registeDans(List<DollRegisterHelper> list,RegisterEvent event){
        list.forEach(helper -> {
            ResourceLocation id =  Res.rl(helper.getName());
            Doll block = new Doll();
            BLOCKS.put(id, block);
            event.register(ForgeRegistries.BLOCKS.getRegistryKey(), id, () -> block);
        });
    }

    private static void registeYichens(List<DollRegisterHelper> list,RegisterEvent event){
        list.forEach(helper -> {
            ResourceLocation id =  Res.rl(helper.getName());
            YiChenDoll block = new YiChenDoll();
            BLOCKS.put(id, block);
            event.register(ForgeRegistries.BLOCKS.getRegistryKey(), id, () -> block);
        });
    }

    private static void registerWithTips(RegisterEvent e, String name, int need_tips_size, int level) {
        ResourceLocation id =  Res.rl( name);
        Doll block = BLOCKS.get(id);
        String[] tip = new String[need_tips_size];
        for (int i = 0; i < need_tips_size; i++) {
            tip[i] = "tip" + (i + 1);
        }
        Item item = new DollItem(block, tip, level);
        ITEMS.add(item);
        e.register(ForgeRegistries.ITEMS.getRegistryKey(), id, () -> item);
    }

    private static void registerSpecialDoll(RegisterEvent e, String name, SoundEvent soundEvent) {
        ResourceLocation id =  Res.rl(name);
        SpecialDoll doll = new SpecialDoll(soundEvent);
        BLOCKS.put(id, doll);
        e.register(ForgeRegistries.BLOCKS.getRegistryKey(), id, () -> doll);
    }
}

class DollRegisterHelper {
    private String name;
    private int level;
    private int need_tips_size;
    private DollRegisterHelper(String name, int level, int need_tips_size){
        this.level = level;
        this.name = name;
        this.need_tips_size = need_tips_size;
    }

    public static DollRegisterHelper getDanFan(String name, int level, int need_tips_size){
        return new DollRegisterHelper(name,level,need_tips_size);
    }

    public String getName() {
        return name;
    }

    public int getNeed_tips_size() {
        return need_tips_size;
    }

    public int getLevel() {
        return level;
    }
}


